import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "首页", icon: "el-icon-food" },
      },
    ],
  },
  {
    path: "/product",
    redirect: "/product/trademark",
    component: () => import("@/layout/index.vue"),
    name: "Product",
    meta: { title: "商品管理", icon: "el-icon-shopping-bag-1" },
    children: [
      {
        path: "/product/trademark",
        name: "Trademark",
        component: () => import("@/views/product/trademark/index.vue"),
        meta: { title: "品牌管理", icon: "el-icon-box" },
      },
      {
        path: "/product/attribute",
        name: "Attribute",
        component: () => import("@/views/product/attribute/index.vue"),
        meta: { title: "平台属性管理", icon: "el-icon-wallet" },
      },
      {
        path: "/product/spu",
        name: "Spu",
        component: () => import("@/views/product/spu/index.vue"),
        meta: { title: "SPU管理", icon: "el-icon-tableware" },
      },
      {
        path: "/product/sku",
        name: "Sku",
        component: () => import("@/views/product/sku/index.vue"),
        meta: { title: "SKU管理", icon: "el-icon-ice-cream" },
      },
    ],
  },
  {
    path: "/user",
    redirect: "/user/userAdmin",
    component: () => import("@/layout/index.vue"),
    name: "User",
    meta: { title: "权限控制", icon: "el-icon-s-custom" },
    children: [
      {
        path: "/user/userAdmin",
        name: "UserAdmin",
        component: () => import("@/views/user/userManagement/index.vue"),
        meta: {
          title: "用户管理",
          icon: "el-icon-user",
        },
      },
      {
        path: "/user/role",
        name: "Role",
        component: () => import("@/views/user/RoleManagement/index.vue"),
        meta: {
          title: "角色管理",
          icon: "el-icon-s-flag",
        },
      },
      {
        path: "/user/role/RoleTree",
        name: "RoleTree",
        component: () => import("@/views/user/RoleManagement/roleTree.vue"),
        meta: {
          title: "角色管理",
          icon: "el-icon-s-custom",
        },
        hidden: true,
      },
      {
        path: "/product/classify",
        name: "Classify",
        component: () => import("@/views/user/ClassifyManagement/index.vue"),
        meta: {
          title: "分类管理",
          icon: "el-icon-user-solid",
        },
      },
    ],
  },
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
